package com.weiaibenpao.demo.chislim.adater;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.bean.Li_MusicResult;
import com.weiaibenpao.demo.chislim.music.bean.Bean_music;

import java.util.ArrayList;

/**
 * Created by lenovo on 2016/10/11.
 */

public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.ViewHolder> {
    private LayoutInflater mInflater;
    private int num = -10;
    int n = 0;
    Context context;

    private ArrayList<Bean_music> mBA_ArrayList;       //本地歌曲
    private ArrayList<Li_MusicResult.DataBean.MusicTypelistBean> mIn_ArrayList;      //网络歌曲Li_MusicResult.DataBean.MusicTypelistBean

    private static OnItemClickListener mOnItemClickListener;
    //设置点击和长按事件

    public interface OnItemClickListener {
        void onItemClick(View view, int position,int state); //设置点击事件
    }
    public static void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    /**
     * 构造器
     * @param context
     */
    public MusicListAdapter(Context context,int n, ArrayList mBA_ArrayList,ArrayList mIn_ArrayList) {
        this.context = context;
        this.n = n;
        this.mInflater = LayoutInflater.from(context);
        this.mBA_ArrayList = mBA_ArrayList;
        this.mIn_ArrayList = mIn_ArrayList;

    }

    /**
     * item显示类型
     * 引入布局
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.list_item, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    /**
     * 数据的绑定显示
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        //绑定事件
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    for(int i = 0;i<mBA_ArrayList.size();i++){
                        if((Integer)holder.itemView.getTag() == 1){
                            mOnItemClickListener.onItemClick(holder.itemView, i,1);
                        }else{
                            mOnItemClickListener.onItemClick(holder.itemView, position,0);
                        }
                    }
                }
            });
        }
        holder.music_Name.setText(mIn_ArrayList.get(n-1).getMusicList().get(position).getMusicName());
        holder.singer_Name.setText(mIn_ArrayList.get(n-1).getMusicList().get(position).getMusicSinger());

        if(mBA_ArrayList.size() == 0){
            holder.state_music.setImageResource(R.mipmap.down_music);
            holder.itemView.setTag(0);                                            //表示未下载
            return;
        }

        for(int i = 0;i<mBA_ArrayList.size();i++){
            Log.i("本地歌曲",mBA_ArrayList.get(i).getMusic_title() + "----" + mIn_ArrayList.get(n-1).getMusicList().get(position).getMusicName());
            if(num == position){
                Glide.with(context)
                        .load(R.mipmap.progress_gif)
                        .asGif()
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                        .override(80,80)
                        .into(holder.state_music);
                break;
            }else if(num == -10){
                if(mIn_ArrayList.get(n-1).getMusicList().get(position).getMusicName().equals(mBA_ArrayList.get(i).getMusic_title())){
                    holder.state_music.setImageResource(R.mipmap.logo_music);
                    holder.itemView.setTag(1);                                            //表示已经下载
                    break;
                }else{
                    holder.state_music.setImageResource(R.mipmap.down_music);
                    holder.itemView.setTag(0);                                            //表示未下载
                }
            }
        }
    }

    //刷新
    public void ReFreshen(ArrayList arrayList){
        mBA_ArrayList = arrayList;
        notifyDataSetChanged();
    }

    //设置下载动画
    public void setAnimationView(int n){
        num = n;
    }
    /**
     * 要显示的item数目
     * @return
     */
    @Override
    public int getItemCount(){
        // return 2;
        return  mIn_ArrayList.get(n-1).getMusicList().size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView state_music,mImage;
        public TextView music_Name;
        public TextView singer_Name;

        public ViewHolder(View view) {
            super(view);

            mImage = (ImageView) view.findViewById(R.id.mImage);
            state_music = (ImageView) view.findViewById(R.id.state_music);
            music_Name = (TextView) view.findViewById(R.id.music_Name);
            singer_Name = (TextView) view.findViewById(R.id.singer_Name);
        }
    }
}